package com.example.demo2.service.impl;

import com.example.demo2.mapper.RepairMapper;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.pojo.data.SupplierDO;
import com.example.demo2.utils.DataUtils;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.data.RepairDO;
import com.example.demo2.pojo.vo.RepairVO;
import com.example.demo2.service.RepairService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/** 
 * <br/>
 * Created by weidong on 2020/02/22
 */
@Service("repairService")
public class RepairServiceImpl implements RepairService {

	@Resource
	private RepairMapper repairMapper;

	@Override 	public Result add(RepairVO repairVO) throws Exception {
		Integer count = repairMapper.add(repairVO);
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getSuccess("添加失败");
		}
	}

	@Override
	public Result updateById(RepairVO repairVO) throws Exception {
		Integer count = repairMapper.updateById(repairVO);
		if (count > 0) {
			return Result.getSuccess("操作成功");
		}else{
			return Result.getSuccess("操作失败");
		}
	}
	
	@Override
	public Result findById(Integer id) throws Exception{
		RepairDO repairDO = repairMapper.findById(id);
		if (repairDO != null) {
			Result result = new Result();
			result.setMsg("操作成功");
			result.setData(repairDO);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}

	@Override
	public Result list(RepairVO repairVO) throws Exception{
		List<RepairDO> repairDOs = repairMapper.list(repairVO);
		if (repairDOs != null) {
			Result result = new Result();
			result.setMsg("查询成功");
			result.setData(repairDOs);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}
	int pageSize=10;
	int navigatePages=3;
	@Override
	public PageBean listPage(RepairVO repairVO) throws Exception{
		//设置分页数据大小
		repairVO.setPageSize(pageSize);
		//查询数目的总个数
		Integer count=repairMapper.count(repairVO);
		if(count==0){
			PageBean pageBean=new PageBean(null,pageSize,repairVO.getCurrentPage(),count,navigatePages);
			//数据库没有数据
			return pageBean;
		}

		//判断页码是否越界,如果页码大于最大值,返回最后一页数据
		//计算最大页码数
		int max= DataUtils.getPageCount(count,pageSize);
		if(repairVO.getCurrentPage()>max){
			repairVO.setCurrentPage(max);
		}

		//设置查询起始位置
		repairVO.setStartRecord(DataUtils.getStartRecord(repairVO.getCurrentPage(),pageSize));

		//调用mapper查询数据
		List<RepairDO> flatsDOS=repairMapper.listPage(repairVO);
		PageBean pageBean=new PageBean(flatsDOS,pageSize,repairVO.getCurrentPage(),count,navigatePages);

		return pageBean;
	}
	
	@Override
	public Result count(RepairVO repairVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteById(Integer id) throws Exception{
		Integer count = repairMapper.deleteById(id);
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getFailure("操作失败");
		}
	}

}